package com.ftg.learn.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * @author Guo
 * @date 2022/3/7
 */
@CrossOrigin(origins = "http://26.234.93.45")
public class BaseController<E, T extends IService<E>> {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    public T service;

    ResEntity re = new ResEntity();

    @GetMapping("/showAll")
    @ApiOperation(value = "通用无条件查询功能")
    public ResEntity showAll() {
        return re.OK(service.list());
    }

    @GetMapping("/showByParam")
    @ApiOperation(value = "按一个条件查询功能")
    public ResEntity showByParam(String column, String val) {
        QueryWrapper<E> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(column, val);
        List<E> list = service.list(queryWrapper);
        return re.OK(list);
    }

    @GetMapping("/showByParams")
    @ApiOperation(value = "按多个条件查询功能")
    public ResEntity showByParam(String[] column, String[] val) {
        QueryWrapper<E> queryWrapper = new QueryWrapper<>();
        for (int i = 0, len = column.length; i < len; i++) {
            queryWrapper.like(column[i], val[i]);
        }
        List<E> list = service.list(queryWrapper);
        return re.OK(list);
    }

    @GetMapping("/showOneById/{id}")
    @ApiOperation(value = "按ID查询一个")
    public ResEntity showOne(@PathVariable("id") Integer id) {
        E one = service.getById(id);
        return re.OK(one);
    }

    @GetMapping("/showOneByParam/{column}/{val}")
    @ApiOperation(value = "按参数查询一个")
    public ResEntity showOne(@PathVariable("column") String column, @PathVariable("val") String val) {
        QueryWrapper<E> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, val);
        E one = service.getOne(queryWrapper);
        return re.OK(one);
    }

    @PostMapping("/insert")
    @ApiOperation(value = "增加功能")
    public ResEntity insert(@RequestBody E e) {
        return service.save(e) ? re.OK() : re.Failure();
    }

    @PostMapping("/insertOrUpdate")
    @ApiOperation(value = "增加或修改功能")
    public ResEntity insertOrUpdate(@RequestBody E e) {
        return service.saveOrUpdate(e) ? re.OK() : re.Failure();
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除功能")
    public ResEntity deleteById(@PathVariable("id") int id) {
        return service.removeById(id) ? re.OK() : re.Failure();
    }

    @DeleteMapping("/delids")
    @ApiOperation(value = "多选删除功能")
    public ResEntity deleteById(@RequestParam List<Serializable> ids) {
        return service.removeBatchByIds(ids) ? re.OK() : re.Failure();
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改功能")
    public ResEntity updateById(@RequestBody E e) {
        return service.updateById(e) ? re.OK() : re.Failure();
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查看")
    public ResPageEntity showByPage(@RequestParam int page, @RequestParam int limit) {
        IPage<E> iPage = new Page<>(page, limit);
        service.page(iPage);
        return new ResPageEntity(iPage);
    }

    @PostMapping("/pageByParam")
    @ApiOperation(value = "By参数分页查看")
    public ResPageEntity showByPageParam(@RequestParam int page, @RequestParam int limit,
                                         @RequestParam String column, @RequestParam String val) {
        IPage<E> ip = new Page<>(page, limit);
        QueryWrapper<E> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(column, val);
        service.page(ip, queryWrapper);
        return new ResPageEntity(ip);
    }

}
